home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech's Sprocket™ / Sprocket (original from 1994) / Sprocket / Lib / AppleEventHandling.cp next >
Encoding:
Text File  |  1994-11-23  |  9.1 KB  |  310 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        AppleEventHandling.cp
  3.  
  4.     Contains:    Minimalist support for the required and Display Manager AppleEvents
  5.                 We also support openning and printing “LetterSpec” documents for AOCE.
  6.                 
  7.                 To really support AppleScript™, we’ll need to do ALOT more work in here.
  8.  
  9.     Written by: Dave Falkenburg
  10.  
  11.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  12.  
  13.     Change History (most recent first):
  14.      
  15.          <6>    11/23/94    DRF        InstallAppleEventHandlers is now called InitAppleEventRoutines.
  16.                                     Also added a useful global: gThisProcessDesc for self targetting
  17.                                     of AppleEvents.
  18.          <5>    11/16/94    DRF        Add StandardAEIdleProc for people who like using AESend with
  19.                                     kAEWaitReply.
  20.          <4>    11/12/94    DRF        Removed extra #include.
  21.          <3>     9/27/94    DRF         AppLib.h is now Sprocket.h
  22.          <2>      9/4/94    DRF        Added stub Text Services AppleEvent handlers.
  23.  */
  24.  
  25. #include "Sprocket.h"
  26.  
  27. #include <AppleEvents.h>
  28. #include <Errors.h>
  29. #include <Displays.h>            //    for Display Manager AppleEvent constants
  30. #include <OCEStandardMail.h>    //    for LetterSpec
  31.  
  32. #if    qInlineInputAware
  33. #include <TextServices.h>
  34. #endif
  35.  
  36. #include "AppleEventHandling.h"
  37.  
  38. static pascal Boolean StandardAEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn);
  39.     
  40.  
  41. AEDesc        gThisProcessDesc;
  42.  
  43. void
  44. InitAppleEventRoutines(void)
  45.     {
  46.     ProcessSerialNumber    thisProcess;
  47.  
  48.     //    Create useful AE Targets for our aplication
  49.  
  50.     //    By building the target in this fashion, we get the best performance
  51.     //    out of the AppleEvent manager because it can call our event handlers
  52.     //    inline.
  53.  
  54.     thisProcess.highLongOfPSN     = 0;
  55.     thisProcess.lowLongOfPSN     = kCurrentProcess;
  56.     (void) AECreateDesc(typeProcessSerialNumber,(Ptr)&thisProcess, sizeof(ProcessSerialNumber),&gThisProcessDesc);
  57.  
  58.  
  59.     //    It’s probably more efficient to use a table to install these handlers…
  60.     
  61.     (void) AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,NewAEEventHandlerProc(HandleOpenApplication),0,false);
  62.     (void) AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,NewAEEventHandlerProc(HandleOpenDocuments),0,false);
  63.     (void) AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,NewAEEventHandlerProc(HandlePrintDocuments),0,false);
  64.     (void) AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,NewAEEventHandlerProc(HandleQuitApplication),0,false);
  65.  
  66.     //    regardless of whether or not we have the display manager, go ahead and register an AppleEvent handler
  67.     (void) AEInstallEventHandler(kCoreEventClass,kAESystemConfigNotice,NewAEEventHandlerProc(HandleSystemConfigNotice),0,false);
  68.  
  69. #if    qInlineInputAware
  70.     if (gHasTextServices)
  71.         {
  72.         (void) AEInstallEventHandler(kTextServiceClass,kUpdateActiveInputArea,NewAEEventHandlerProc(HandleTextServicesUpdateActiveInputArea),0,false);
  73.         (void) AEInstallEventHandler(kTextServiceClass,kPos2Offset,NewAEEventHandlerProc(HandleTextServicesPos2Offset),0,false);
  74.         (void) AEInstallEventHandler(kTextServiceClass,kOffset2Pos,NewAEEventHandlerProc(HandleTextServicesOffset2Pos),0,false);
  75.         //    we don’t need to handle <kTextServiceClass,kShowHideInputWindow> events
  76.         }
  77. #endif
  78.     }
  79.  
  80.  
  81. OSErr
  82. CheckAppleEventForMissingParams(AppleEvent *theAppleEvent)
  83.     {
  84.     DescType    returnedType;
  85.     Size        actualSize;
  86.     OSErr        err;
  87.     
  88.     err = AEGetAttributePtr(theAppleEvent,keyMissedKeywordAttr,typeWildCard,
  89.                             &returnedType,nil,0,&actualSize);
  90.     
  91.     if (err == errAEDescNotFound)        //    If we couldn’t find the error attribute
  92.         return noErr;                    //        everything is ok, return noErr
  93.     
  94.     if (err == noErr)                    //    We found an error attribute, so
  95.         return errAEEventNotHandled;    //        tell the client we ignored the event
  96.  
  97.     return err;                            //    Something else happened, return it
  98.     }
  99.  
  100.  
  101. AEIdleUPP        StandardAEIdleUPP = NewAEIdleProc(StandardAEIdleProc);
  102.  
  103. static pascal Boolean
  104. StandardAEIdleProc(EventRecord *theEvent, long * /* sleepTime */, RgnHandle * /* mouseRgn */)
  105.     {
  106.     HandleEvent(theEvent);            //    First, always hand event off to our event handling code
  107.     return false;                    
  108.     }
  109.  
  110.  
  111. OSErr
  112. ForEachDocumentInList(AEDescList documentList,EachDocumentProcPtr documentProc,void * documentParam)
  113.     {
  114.     long                documentCount,documentIndex;
  115.     DescType            returnedType;
  116.     Size                actualSize;
  117.     LetterDescriptor    theLetterDesc;
  118.     AEKeyword            keyword;
  119.     OSErr                err;
  120.  
  121.     if ((err = AECountItems(&documentList,&documentCount)) != noErr)
  122.         return err;
  123.     
  124.     for (documentIndex=1; documentIndex <= documentCount; documentIndex++)
  125.         {
  126.         //    What kind of document is it?
  127.         if ((err = AESizeOfNthItem(&documentList,documentIndex,&returnedType,&actualSize)) != noErr)
  128.             return err;
  129.         
  130.         //    Is it an AOCE letter?
  131.         if (returnedType == typeLetterSpec)
  132.             {
  133.             //    It’s a letter
  134.             theLetterDesc.onDisk = false;
  135.             err = AEGetNthPtr(&documentList,documentIndex,typeLetterSpec,&keyword,&returnedType,
  136.                                 (Ptr) &theLetterDesc.u.mailboxSpec, sizeof(theLetterDesc.u.mailboxSpec),&actualSize);
  137.             }
  138.         else
  139.             {
  140.             //    It’s just a normal document file
  141.             theLetterDesc.onDisk = true;
  142.             err = AEGetNthPtr(&documentList,documentIndex,typeFSS,&keyword,&returnedType,
  143.                                 (Ptr) &theLetterDesc.u.fileSpec,sizeof(theLetterDesc.u.fileSpec),&actualSize);
  144.             }
  145.             
  146.         if (err == noErr)
  147.             (*documentProc)(&theLetterDesc,documentParam);
  148.         else
  149.             break;
  150.         }
  151.     
  152.     return    err;
  153.     }
  154.  
  155.     
  156. pascal OSErr
  157. HandleOpenApplication(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  158.     {
  159.     OSErr    err;
  160.     
  161.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  162.         return err;
  163.  
  164.     return(CreateNewDocument());
  165.     }
  166.  
  167.  
  168. pascal OSErr
  169. HandleOpenDocuments(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  170.     {
  171.     AEDescList            documentList;
  172.     OSErr                err;
  173.     
  174.     if ((err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&documentList)) != noErr)
  175.         return err;
  176.  
  177.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  178.         return err;
  179.  
  180.     err = ForEachDocumentInList(documentList,&OpenDocument,nil);
  181.     (void) AEDisposeDesc(&documentList);
  182.     return err;
  183.     }
  184.  
  185.  
  186. pascal OSErr
  187. HandlePrintDocuments(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  188.     {
  189.     AEDescList            documentList;
  190. #if    qUseQuickDrawGX
  191.     AEDescList            desktopPrinterList;
  192.     FSSpec                thePrinterFSSpec;
  193.     Boolean                draggedToDesktopPrinter = false;
  194. #endif
  195.     OSErr                err;
  196.     
  197.     if ((err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&documentList)) != noErr)
  198.         return err;
  199.  
  200. #if    qUseQuickDrawGX
  201.     if (noErr == AEGetAttributeDesc(theAppleEvent,keyOptionalKeywordAttr,typeAEList,&desktopPrinterList))
  202.         draggedToDesktopPrinter = true;
  203. #endif
  204.  
  205.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  206.         return err;
  207.  
  208. #if    qUseQuickDrawGX
  209.     if (draggedToDesktopPrinter)
  210.         {
  211.         DescType            returnedType;
  212.         Size                actualSize;
  213.         AEKeyword            keyword;
  214.  
  215.         err = AEGetNthPtr(&desktopPrinterList,1,typeFSS,&keyword,&returnedType,
  216.                             (Ptr) &thePrinterFSSpec,sizeof(thePrinterFSSpec),&actualSize);
  217.  
  218.         (void) AEDisposeDesc(&desktopPrinterList);
  219.         }
  220.     err = ForEachDocumentInList(documentList,&PrintDocument,draggedToDesktopPrinter ? &thePrinterFSSpec : NULL);
  221.     (void) AEDisposeDesc(&documentList);
  222. #else
  223.     err = ForEachDocumentInList(documentList,&PrintDocument,nil);
  224. #endif
  225.  
  226.     return err;
  227.     }
  228.  
  229.  
  230. pascal OSErr
  231. HandleQuitApplication(AppleEvent *theAppleEvent,AppleEvent * /* reply */,long /* refCon */)
  232.     {
  233.     OSErr    err;
  234.     
  235.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  236.         return err;
  237.  
  238.     gDone = QuitApplication();
  239.     
  240.     if (gDone)
  241.         return noErr;
  242.     else
  243.         return userCanceledErr;
  244.     }
  245.  
  246.  
  247. ////////////////////////////////////////////////////////////////////////
  248. //
  249. //    Display Manager Suite is “Under Construction”
  250. //
  251. //    To Do: Check ERS for Display Manager events
  252.  
  253. pascal OSErr
  254. HandleSystemConfigNotice(AppleEvent *theAppleEvent,AppleEvent * /* reply */,long /* refCon */)
  255.     {
  256.     AEDescList    displayList;
  257.     long        displayCount,displayIndex;
  258.     OSErr        err;
  259.     
  260.     DebugStr((StringPtr) "\pGot a Display Manager Event!");
  261.     
  262.     if ((err = AEGetParamDesc(theAppleEvent,kAEDisplayNotice,typeAEList,&displayList)) != noErr)
  263.         return err;
  264.     
  265.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  266.         return err;
  267.     
  268.     if ((err = AECountItems(&displayList,&displayCount)) != noErr)
  269.         return err;
  270.  
  271.     for (displayIndex = 1; displayIndex<=displayCount; displayIndex++)
  272.         {
  273.         DebugStr((StringPtr) "\pProcessing a display");
  274.         
  275.         //    Gather up all the old and new display rectangles into an oldDeskRegion and a newDeskRegion
  276.         }
  277.  
  278.     //    run through all windows calling keeping all windows which were onscreen in the oldDeskRegion
  279.     //    onscreen in the new world. We should really be calling a method to do this so that windows
  280.     //    can individually deal with things in a manner which they can override.
  281.  
  282.  
  283.     return errAEEventNotHandled;    //    we really don’t handle this yet...
  284.     }
  285.  
  286. #if    qInlineInputAware
  287.  
  288. pascal OSErr
  289. HandleTextServicesUpdateActiveInputArea(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  290.     {
  291.     DebugStr("\pTextServicesUpdateActiveInputArea");
  292.     return errAEEventNotHandled;            //    we really don’t handle this yet...
  293.     }
  294.  
  295. pascal OSErr
  296. HandleTextServicesPos2Offset(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  297.     {
  298.     DebugStr("\pTextServicesPos2Offset");
  299.     return errAEEventNotHandled;            //    we really don’t handle this yet...
  300.     }
  301.     
  302. pascal OSErr
  303. HandleTextServicesOffset2Pos(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  304.     {
  305.     DebugStr("\pTextServicesOffset2Pos");
  306.     return errAEEventNotHandled;            //    we really don’t handle this yet...
  307.     }
  308.  
  309. #endif
  310.